/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-10-17 11:15:26
 * @LastEditTime: 2022-10-24 16:38:13
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import {
  ADD_TEMPLATETASK_LIST,
  GET_TEMPLATETASK_LIST,
  DELETE_TEMPLATETASK_ITEM,
  EDIT_TEMPLATETASK,
  SET_ARCHIVETYPE,
  CLEAR_TEMPLATE
} from '../constants';
import { ArchivesDetectionReducer } from '../reducers/ArchivesDetectionReducer';
import {
  addDetectionTemplate,
  detectionTemplateList,
  deleteDetectionTemplate,
  editeDetectionTemplate
} from '@/api/standardizedDetection/detectionTemplate';
import { fetchATListByFondId } from '@/api/archivesType';

import { message, Modal } from 'antd';

const initialState = {
  DetectionTemplate: [],
  archiveTypeList: [],
  DetectionTemplateId: null
};
const ArchivesDetectionContext = createContext();
const ArchivesDetectionContextProvider = (props) => {
  const [state, dispatch] = useReducer(ArchivesDetectionReducer, initialState);
  const actions = {
    /* 添加检测模板 */
    addTemplateTaskList(params) {
      return new Promise((resolve, reject) => {
        addDetectionTemplate({ ...params })
          .then((res) => {
            message.success('创建成功', res);
            const { data } = res;
            dispatch({ type: ADD_TEMPLATETASK_LIST, data });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    /* 查询对应档案类型所有的检测模板 */
    /**
     * @description:
     * @param {*} params
     * @return {*}
     */
    searchTemplateTaskList(params) {
      return new Promise((resolve, reject) => {
        detectionTemplateList(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: GET_TEMPLATETASK_LIST, data });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    /* 删除检测模板 */
    deleteItem(id) {
      Modal.confirm({
        title: '是否移除此模板?',
        content: '是否移除此模板？移除后不可恢复',
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          deleteDetectionTemplate(ids).then((res) => {
            const data = res.data.map((id) => parseInt(id, 10));
            message.success('移除成功');
            dispatch({ type: DELETE_TEMPLATETASK_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('del item Cancel');
        }
      });
    },
    /* 修改检测模板 */
    editTemplateTask(params) {
      return new Promise((resolve, reject) => {
        editeDetectionTemplate(params)
          .then((res) => {
            const { data } = res;
            const { id } = res.data;
            dispatch({ type: EDIT_TEMPLATETASK, id, data });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },
    /* 查询当前全宗的档案类型 */
    setArchiveType(selectFondId) {
      fetchATListByFondId(selectFondId).then((res) => {
        const { data } = res;
        dispatch({ type: SET_ARCHIVETYPE, data });
      });
    },
    /* 关闭时清空检测模板 */
    clearDetectionTemplate() {
      dispatch({ type: CLEAR_TEMPLATE });
    }
  };
  useEffect(() => {}, []);
  return (
    <ArchivesDetectionContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </ArchivesDetectionContext.Provider>
  );
};
export const useArchivesDetectionContext = () => {
  return useContext(ArchivesDetectionContext);
};
export { ArchivesDetectionContext, ArchivesDetectionContextProvider };
